/**
 * @file categories/graph/topological_sort/luogu3074/main.cpp
 * @brief
 * @see
 * @author Ruiming Guo (guoruiming@stu.scu.edu.cn)
 * @copyright 2022
 * @date 2022/7/8 15:23:29
 **/

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef vector<int> vi;
const int N = 10010;
int t[N];
int timeWait[N];
int inDeg[N];
vi outEdge[N];
int n, m;
int main() {
  // High rating and good luck!
  cin >> n >> m;
  for (int i = 1; i <= n; ++i) cin >> t[i];
  for (int i = 1; i <= m; ++i) {
    int a, b;
    cin >> a >> b;
    outEdge[a].push_back(b);
    inDeg[b]++;
  }
  queue<int> q;
  for (int i = 1; i <= n; ++i) {
    if (inDeg[i] == 0) {
      // 关键
      timeWait[i] = t[i];
      q.push(i);
    }
  }
  while (q.size()) {
    int u = q.front();
    q.pop();
    for (auto v : outEdge[u]) {
      inDeg[v]--;
      if (inDeg[v] == 0) q.push(v);
      // 关键
      timeWait[v] = max(timeWait[v], timeWait[u] + t[v]);
    }
  }
  cout << *max_element(timeWait, timeWait + n) << endl;
  return 0;
}
